import { _decorator, Color, Component, tween } from 'cc';
const { ccclass, property } = _decorator;
import * as fgui from "fairygui-cc";
import { GameController } from '../manager/GameController';
import { AudioManager } from '../manager/AudioManager';

export default class ProgressCtrl {

    private _node: fgui.GComponent;

    public aniController: fgui.Controller = null;
    private _progress: fgui.GProgressBar = null;

    private _nameLabel: fgui.GTextField = null;
    private _energyLabel: fgui.GTextField = null;
    private _roundUpEnergyLabel: fgui.GComponent = null;

    private _ani: fgui.GComponent = null;
    private _aniX: number = 0;

    private _upEnergyTipShow: fgui.Controller = null;

    constructor(node: fgui.GComponent, upEnergyTipShow: fgui.Controller, roundUpEnergyLabel: fgui.GComponent) {
        this._node = node;
        this._upEnergyTipShow = upEnergyTipShow;
        this._progress = this._node.getChild("n12", fgui.GProgressBar);
        this._roundUpEnergyLabel = roundUpEnergyLabel;

        this.aniController = this._node.getController('c1');//抽牌能量动画
        this._nameLabel = this._node.getChild('n6', fgui.GTextField)
        this._energyLabel = this._node.getChild('n5', fgui.GTextField)
        this._ani = this._progress.getChild('ani').asCom
        this._aniX = this._ani.x;
    }

    public updataEnergy(updateEnergy: number) {
        let energy = GameController.Instance.currentEnergy
        let duration = 0.5;
        let bar = this._progress.getChild("bar").asCom;
        this._progress.tweenValue(energy, duration)
        this._energyLabel.text = `${energy}`

        if (energy > 0) {
            this._ani.visible = true;
            let targetX = this._aniX + bar.width * (Math.min(30, energy) / 30.0) + 8;
            tween(this._ani).to(duration, { x: targetX }).start();
        }

        if (updateEnergy != 0) {
            this._roundUpEnergyLabel.text = `${updateEnergy}`
            AudioManager.Instance.playEffectWithName('fire');
            setTimeout(() => {
                this._upEnergyTipShow.setSelectedIndex(1)
            }, 500);

            setTimeout(() => {
                this._upEnergyTipShow.setSelectedIndex(0)
            }, 1500);
        }
    }

    public updateInfo() {

        console.log(`[${new Date().toLocaleTimeString()}] + updateInfo`, GameController.Instance.currentEnergy)

        const nickName = GameController.Instance.nickName;
        this._nameLabel.text = nickName;

        let energy = GameController.Instance.currentEnergy
        this._energyLabel.text = `${energy}`
        this._progress.tweenValue(energy, 0.2)
        let bar = this._progress.getChild("bar").asCom;

        setTimeout(() => {
            if (energy > 0) {
                this._ani.visible = true;
            }
            let targetX = this._aniX + bar.width * (Math.min(30, energy) / 30.0) + 8;
            this._ani.x = targetX;
        }, 100);
    }

    public roundUpdateEvent() {

        if (this._nameLabel.text != GameController.Instance.nickName) {
            this._nameLabel.text = GameController.Instance.nickName;
        }

        let energy = GameController.Instance.currentEnergy
        if (energy > 30) {
            this.aniController.setSelectedIndex(2)
        }
        else {
            this.aniController.setSelectedIndex(0)
        }
        this._energyLabel.text = `${energy}`
    }

    public reset() {
        this._energyLabel.text = '0'
        this._progress.tweenValue(0, 0)
        this._ani.x = this._aniX;
        this._ani.visible = false;
        this.aniController.setSelectedIndex(0);
    }
}
